package com.xmut.mall.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.xmut.mall.member.annotation.CheckJwt;
import com.xmut.mall.member.utils.RespBean;
import com.xmut.mall.member.vo.CollectProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xmut.mall.member.entity.MemberCollectSkuEntity;
import com.xmut.mall.member.service.MemberCollectSkuService;
import com.xmut.mall.member.utils.PageUtils;
import com.xmut.mall.member.utils.R;


/**
 * 会员收藏的商品
 *
 * @author 1912120131suntingxing
 * @email 1192081604@qq.com
 * @date 2022-07-14 01:20:46
 */
@RestController
@RequestMapping("member/membercollectspu")
public class MemberCollectSkuController {
    @Autowired
    private MemberCollectSkuService memberCollectSpuService;

    /**
     * 获取用户收藏商品的分页数据
     * @param params
     * @return
     */
    @CheckJwt
    @PostMapping("/getUserCollectionByPage")
    public RespBean getUserCollectionByPage(@RequestBody Map<String,Object> params){
        // 因为pageUtils无法使用springcache反序列化 所以使用返回对象进行缓存
        return memberCollectSpuService.getUserCollectionByPage(params);

    }

    /**
     * 用户收藏商品
     * @return
     */
    @CheckJwt
    @PostMapping("/collectProduct")
    public RespBean collectProduct(@RequestBody CollectProductVo vo){
        System.out.println(vo);
        return memberCollectSpuService.collectProduct(vo);

    }

    /**
     * 用户取消收藏商品
     * @param userId
     * @param skuId
     * @return
     */
    @CheckJwt
    @DeleteMapping("/cancelCollection/{userId}/{skuId}")
    public RespBean cancelCollection(@PathVariable("userId") Long userId,@PathVariable("skuId")Long skuId){
        System.out.println("测试:"+userId+" "+skuId);
        Boolean result = memberCollectSpuService.cancelCollection(userId,skuId);
        if(result){
            return RespBean.success("取消收藏成功");
        }else {
            return RespBean.error("取消收藏失败");
        }
    }


    /**
     * 检查用户是否收藏了该商品
     * @param userId
     * @param skuId
     * @return
     */
    @CheckJwt
    @GetMapping("/isColletcion/{userId}/{skuId}")
    public RespBean isColletcion(@PathVariable("userId") Long userId,@PathVariable("skuId")Long skuId){
        Boolean result = memberCollectSpuService.isColletcion(userId,skuId);
        if (result){
            return RespBean.success("已收藏");
        }else {
            return RespBean.error("未收藏");
        }
    }

    /**
     * 根据用户Id获取该用户收藏的商品
     * @param userId
     * @return
     */
    @CheckJwt
    @GetMapping("/getCollectionSpuByUserId/{userId}")
    public RespBean getCollectionSpuByUserId(@PathVariable Long userId) {
        List<MemberCollectSkuEntity> spus = memberCollectSpuService.getCollectionSpuByUserId(userId);
        return RespBean.success("获取用户收藏商品成功",spus);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberCollectSpuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberCollectSkuEntity memberCollectSpu = memberCollectSpuService.getById(id);

        return R.ok().put("memberCollectSpu", memberCollectSpu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberCollectSkuEntity memberCollectSpu) {
        memberCollectSpuService.save(memberCollectSpu);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberCollectSkuEntity memberCollectSpu) {
        memberCollectSpuService.updateById(memberCollectSpu);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberCollectSpuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
